package com.learning.concurrent.basic.designpattern.masterworker;

import lombok.extern.slf4j.Slf4j;
import org.omg.SendingContext.RunTime;

import java.util.Random;

/**
 * description:
 *
 * @author minghuiZhang
 * @date created in  16:05 2018/5/15
 * modified by
 */
@Slf4j
public class Test {
	public static void main(String[] args) {
		int threadCount = Runtime.getRuntime().availableProcessors();
		log.info("当前机器线程可用数：{}" ,threadCount);
		Master master = new Master(new Worker(),threadCount);
		Random random = new Random();
		int count = 100;
		for (int i = 1; i <= count; i++) {
			Task task = new Task();
			task.setId(i);
			task.setName("task" + i);
			task.setPrice(random.nextInt(1000));
			master.submit(task);
		}
		master.execute();
		long start = System.currentTimeMillis();
		while(true){
			if (master.isComplete()){
				long end = System.currentTimeMillis() - start;
				log.info("多个worker线程完成所有任务用时：{}",end);
				int result = master.getResult();
				log.info("最终结果是：{}",result);
				break;
			}
		}

	}
}
